

var gs3 = require('gs3');
var Entity = gs3.Models.Entity;
var Utilities = gs3.Utilities;
//var Messaging_Management = gs3.Modules.Messaging.Messaging_Management;
var Live_Messaging_Management = gs3.Modules.Messaging.Live_Messaging_Management;

var db_config = require('./db_config');

var messageTable = {
    tableName: "message",
    primary: "id",
    author: "Author_id",
    dateCreated: "Date_Created"    
};

var userTable = {
    tableName: "user",
    primary: "id"
};

var userHasMessageTable = {
    tableName: "user_has_message",
    messageId: "Message_id",
    userId: "User_id"
};

var chatMessageTypeId = 2;

//  Instance Scoped (relative to Discussions_Abstract) Inheritance
//var Chat_Impl = new Live_Messaging_Management(db_config, messageTable, userTable, userHasMessageTable);
Live_Messaging_Impl = new Live_Messaging_Management(db_config, messageTable, userTable, userHasMessageTable);

// <editor-fold defaultstate="collapsed" desc="Override Methods. Click on the + sign on the left to edit the code.">

Live_Messaging_Impl.modifyMessageOnClientSend = function(messageEntity) {
    var newEntity = messageEntity;

    //  Override Purpose
    //  Add the id for chat messages (1 = Message, 2 = chat, 3 = blog, 4 = discussion)
    //  This is according to the database design
    newEntity.fields[messageTable.messageTypeId] = chatMessageTypeId;

    return messageEntity;
};

Live_Messaging_Impl.getLimitedConversation = function(userId, friendId, lowerLimit, upperLimit, callback) {
    var selection = "um.User_id AS Receiptient_id, m.Author_id AS Sender_id, m.id, m.Date_Created, m.Message, u.Email, u.Name";

    var selectQuery = "SELECT " + selection + " FROM " + this.messageTable.name + " m " +
            "LEFT JOIN " + this.userHasMessageTable.name + " um " +
            "ON m." + this.messageTable.primary + "=um." + this.userHasMessageTable.messageId + " " +
            "LEFT JOIN " + this.userTable.name + " u " +
            "ON u." + this.userTable.primary + "=m." + this.messageTable.author + " " +
            "WHERE " +
            "(m." + this.messageTable.author + "='" + userId + "' AND um." + this.userHasMessageTable.userId + "='" + friendId + "') " +
            "OR " +
            "(m." + this.messageTable.author + "='" + friendId + "' AND um." + this.userHasMessageTable.userId + "='" + userId + "') " +
            "ORDER BY " + this.messageTable.dateCreated + " DESC LIMIT " + lowerLimit + ", " + upperLimit;

    var db = this.database;
    db.connect();

    db.query(selectQuery, function(err, data) {
        callback(err, data);
        db.disconnect();
    });
};

Live_Messaging_Impl.getMessageAndAuthor = function(messageId, callback) {
    var selectQuery = "SELECT m.*, u.* FROM " + this.messageTable.name +
            " m LEFT JOIN " + this.userTable.name +
            " u ON m." + this.messageTable.author + "=u." + this.userTable.primary +
            " WHERE m." + this.messageTable.primary + "='" + messageId + "'";

    var db = this.database;
    db.connect();

    db.query(selectQuery, function(err, data) {
        if (err) {
            callback(err, data);
        } else {
            if (data[0].password) {
                delete data[0].password;
            } else if (data[0].Password) {
                delete data[0].Password;
            }                        

            callback(err, data[0]);
        }
        db.disconnect();
    });
};

Live_Messaging_Impl.onUserLogin = function(user) {
    if (user) {
        var userEntity = new Entity(this.userTable.name, this.userTable.primary);

        var db = this.database;
        db.connect();

        userEntity.fields[this.userTable.primary] = user[this.userTable.primary];
        userEntity.fields.Availability = '1';

        db.update(userEntity, function(err, data) {
            if (err) {
                Utilities.log(err.message, Utilities.LOG_ERR);
            } else {
                Utilities.log('User successfully logged in.', Utilities.LOG_INFO);
            }

            db.disconnect();
        });
    }
};

Live_Messaging_Impl.onUserLogout = function(user) {
    if (user) {
        var userEntity = new Entity(this.userTable.name, this.userTable.primary);

        var db = this.database;
        db.connect();

        userEntity.fields[this.userTable.primary] = user[this.userTable.primary];
        userEntity.fields.Availability = '0';

        db.update(userEntity, function(err, data) {
            if (err) {
                Utilities.log(err.message, Utilities.LOG_ERR);
            } else {
                Utilities.log('User successfully logged out.', Utilities.LOG_INFO);
            }

            db.disconnect();
        });
    }
};

// </editor-fold>

module.exports = Live_Messaging_Impl;